from django.forms.models import ModelForm
from kalapankki.album.models import Album, Picture, Page
from django import forms
from django.contrib.auth.models import User
from kalapankki.settings import STATIC_URL

class CreateAlbumForm(ModelForm):
    class Meta:
        model = Album
        fields = ('name','description','public', 'cover_picture')
        
    def clean(self):
        if self.cleaned_data['cover_picture'] is None \
        or self.cleaned_data['cover_picture'] == "":
            self.cleaned_data['cover_picture'] = self._default_cover_picture()
        return ModelForm.clean(self)
    
    def _default_cover_picture(self):
	try:
 	    user = User.objects.get(username="TeemuTeekkari")
	except User.DoesNotExist:
            user = User.objects.create_user("TeemuTeekkari", "none@example.com", "randomizeMeForRealz")
	picture = Picture(uri=STATIC_URL + "default_cover.png", name="Default cover picture", owner=user)
	picture.save()
	return picture
    
class AddPageForm(ModelForm):
    class Meta:
        model = Page
        fields = ('caption','description','layout')

class AddPictureForm(ModelForm):
    class Meta:
        model = Picture
        fields = ('uri', 'name', 'description')

class UploadPictureForm(ModelForm):
    file = forms.FileField()
    class Meta:
        model = Picture
        fields = ('name', 'description')
